package com.yanq.liancar.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.bean.UserBindBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SmallBBindAccountActivity extends BaseToolbarActivity {
    @BindView(R.id.SmallBBindAccount_phone_btn)
    LinearLayout SmallBBindAccountPhoneBtn;
    @BindView(R.id.SmallBBindAccount_weChat_btn)
    LinearLayout SmallBBindAccountWeChatBtn;
    @BindView(R.id.tv_bindphone)
    TextView tvBindphone;
    @BindView(R.id.tv_bindwechat)
    TextView tvBindwechat;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_small_b_bindaccount);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, true, false);
        mTvTitle.setText("账号绑定");
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserStatus();
    }

    @OnClick({R.id.SmallBBindAccount_phone_btn, R.id.SmallBBindAccount_weChat_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.SmallBBindAccount_phone_btn://绑定手机号
                break;
            case R.id.SmallBBindAccount_weChat_btn://绑定微信号
                RxActivityTool.skipActivity(this, SmallBBindingWeChatActivity.class);
                break;
        }
    }

    private void getUserStatus() {
        OkGo.<String>post(Config.s + "llc/queryUserBindWxStatus")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.e("11111111111", response.body().toString());
                        if (!response.equals("")) {
                            Gson gson = new Gson();
                            UserBindBean bean = gson.fromJson(response.body(), UserBindBean.class);
                            if (bean.isIsSuccess()) {
                                if (bean.getData().getOpenid() != null) {
                                    tvBindwechat.setText("已绑定");
                                    SmallBBindAccountWeChatBtn.setEnabled(false);
                                }
                                if (bean.getData().getTel()!=null){
                                    tvBindphone.setText("已绑定");
                                    SmallBBindAccountPhoneBtn.setEnabled(false);
                                }
                            } else {
                                RxToast.error(bean.getOutputMessage());
                            }

                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        RxToast.error("服务器连接失败");
                        LogUtils.e("asdasdad", response.code() + "");
                        LogUtils.e("asdasdad", response.message() + "");
                        super.onError(response);
                    }
                });
    }
}
